<?php
namespace app\workerman;



use app\common\entity\GameLoginLog;
use app\index\validate\Attest;
use GatewayWorker\Lib\Gateway;
use app\common\entity\GameRoom;
use app\workerman\Attached;
use Workerman\Lib\Timer;
use app\workerman\Game;
use app\common\entity\User;
use think\Db;

class Robot extends Attached
{


    public function robotRandIn(){
        Timer::add(0.5,function(){
            $arrr = GameRoom::where('status',0)->select();
            foreach($arrr as $k=>$v){
                $roomCount = Gateway::getClientCountByGroup($v['id']) == 0?0: Gateway::getClientCountByGroup($v['id']);
                $room_redis = $this->redis_hGet('1225blz_roominfo',$v['id']);
                if(!$roomCount && isset($room_redis['users']) && count($room_redis['users']) < $v['num'] && count($room_redis) < 10){
                    //预设值
                    $rand = rand(1,10);
                    if($rand === 1){
                        $user = User::where('robot',1)->where('status',1)->where('game_status',0)->orderRand()->find();
                        if(!$user)continue;
                        $this->robotRoomIn($user['id'],$v['id'],true);
                    }
                }
            }
        });
        Timer::add(30,function() {
            $arrr = GameRoom::where('status', 0)->select();
            foreach ($arrr as $k => $v) {
                $roomCount = Gateway::getClientCountByGroup($v['id']) == 0 ? 0 : Gateway::getClientCountByGroup($v['id']);
                $room_redis = $this->redis_hGet('1225blz_roominfo', $v['id']);

                if ($roomCount >= 1 && count($room_redis['users']) < 10) {
                    #安排机器人进入房间
                    $user = User::where('robot', 1)->where('status', 1)->where('game_status', 0)->orderRand()->find();
                    if (!$user) continue;
                    $this->robotRoomIn($user['id'], $v['id']);
                }
            }
        });
    }

    #进入房间
    public function robotRoomIn($uid,$room_id,$rand = false){


        $room_redis = $this->redis_hGet('1225blz_roominfo',$room_id);
        if(!$room_redis){
            $info = [
                'num'=>1,
                'users'=>[],
            ];
            $this->redis_hSet('1225blz_roominfo',$room_id,$info);//存入redis
            $room_redis = $info;
        }
        if(count($room_redis['users']) >= 10){
            return false;
        }

        if(Gateway::getClientCountByGroup($room_id) == 0 && $rand == false){
            #@解散房间
            return false;
        }else{

            $info = [
                'uid' => $uid,
                'robot' => 1,
                'status' => 1,
                'award' => 0,
            ];

            array_push($room_redis['users'],$info);
            $this->redis_hSet('1225blz_roominfo',$room_id,$room_redis);//存入redis
            User::where('id',$uid)->update(['game_status'=>1]);

        }


        $roomInfo = $room_redis['users'];
        $roomUserArr = [];
        foreach($roomInfo as $k=>$v){
            $roomUserArr[] = $this->getInfo($v['uid']);
        }

        Gateway::sendToGroup($room_id,successSend('userjoin','用户进房',$roomUserArr));
        $arrr = GameRoom::select();
        foreach($arrr as $k=>$v){
            $roomItem = $this->redis_hGet('1225blz_roominfo',$v['id']);
            $v['count'] = isset($the_room['users'])?count($the_room['users']):0;
        }

        $this->roomchange();
    }

    /**
     * 判断是不是机器人
     * @param $uid
     * @return bool
     */
    public function is_robot($uid){
        if(!$uid){
            return false;
        }
        $robot = User::where('id',$uid)->where('robot',1)->find();
        return $robot?true:false;
    }


}